//
//  GoodsDetailHTMLCell.m
//  CenturyCity
//
//  Created by beisheng03 on 2017/12/29.
//  Copyright © 2017年 beisheng. All rights reserved.
//

#import "GoodsDetailHTMLCell.h"
#import <WebKit/WebKit.h>
#import "WKDelegetaController.h"
#import <MobileCoreServices/MobileCoreServices.h>
#import "GoodDetailController.h"
@interface GoodsDetailHTMLCell ()<WKNavigationDelegate,WKScriptMessageHandler,
WKDelegateControllerDelegate>
@property (weak, nonatomic) IBOutlet UIView *topBgView;
@property (weak, nonatomic) IBOutlet UILabel *topLabel;
@property (weak, nonatomic) IBOutlet UIView *wkwebViewBg;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *bottomMargin;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *wkwebBgH;

@property (nonatomic, weak) WKWebView *webView;

/** <#note#> */
@property (nonatomic, strong) NSString *myTag;

@property (nonatomic, strong) WKUserContentController  *userContentController;
@end

@implementation GoodsDetailHTMLCell


- (WKWebView *)webView{
    if (!_webView) {
        //配置环境
        WKWebViewConfiguration *config = [[WKWebViewConfiguration alloc]init];
        
        _userContentController = [[WKUserContentController alloc]init];
        
        config.userContentController = _userContentController;
        
        
        WKWebView *webView = [[WKWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 555.555) configuration:config];
        
        //注册方法
        
        WKDelegetaController *delegateController = [[WKDelegetaController alloc]init];
        
        delegateController.delegate = self;
        
//        // 自适应屏幕宽度js
//        NSString *jSString = @"var meta = document.createElement('meta'); meta.setAttribute('name', 'viewport'); meta.setAttribute('content', 'width=device-width'); document.getElementsByTagName('head')[0].appendChild(meta);";
//        WKUserScript *wkUserScript = [[WKUserScript alloc] initWithSource:jSString injectionTime:WKUserScriptInjectionTimeAtDocumentEnd forMainFrameOnly:YES];
//        // 添加js调用
//        [_userContentController addUserScript:wkUserScript];
        
        
        
        [_userContentController addScriptMessageHandler:delegateController name:@"sendMessage"];//注册一个name为sendMessage的js方法
        
        
//        UIWebView *web = [[UIWebView alloc]initWithFrame:CGRectMake(0, 0, KScreenW, 500)];
        


        _webView = webView;
        
        _webView.userInteractionEnabled = NO;
     

        
        [self.wkwebViewBg addSubview:_webView];

        
    }
    
    return _webView;
    
}


- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.topBgView.backgroundColor = viewBgGrey;
    
    self.topLabel.font = MYFONT(17);
    
    self.topLabel.textColor = darkGray;
    
    self.wkwebBgH.constant = 1000;
    
    self.bottomMargin.constant = 0;
    
    self.wkwebViewBg.backgroundColor = [UIColor whiteColor];
    
    
    self.webView.backgroundColor = [UIColor whiteColor];
    
    
    self.webView.navigationDelegate = self;

    [self.webView addObserver:self forKeyPath:@"estimatedProgress" options:NSKeyValueObservingOptionNew context:nil];
    
    [self.webView addObserver:self forKeyPath:@"title" options:NSKeyValueObservingOptionNew context:nil];
    
    [self.webView.scrollView addObserver:self forKeyPath:@"contentSize" options:NSKeyValueObservingOptionNew context:nil];

    
    self.myTag = @"0";
    
    // Initialization code
}

- (void)dealloc{
    
    //前面加过的方法一定要remove掉
    [_userContentController removeScriptMessageHandlerForName:@"sendMessage"];
    
    [self.webView removeObserver:self forKeyPath:@"estimatedProgress"];
    
    [self.webView removeObserver:self forKeyPath:@"title"];
    
    [self.webView.scrollView removeObserver:self forKeyPath:@"contentSize"];

    
    
    NSLog(@"销毁了...");
}


- (void)setModel:(GoodsDetailModel *)model{
    
    _model = model;
    
//    NSLog(@"%@",_model);
    
//    NSLog(@"9999999999999");
    
    if (_model != nil) {
        
//        NSLog(@"888888888888");


        if ([self.myTag isEqualToString:@"0"]) {

//            NSLog(@"1010101010101010");

            [self.webView loadRequest:[NSURLRequest requestWithURL:[NSURL URLWithString:_model.content]]];

            
            self.myTag = @"1";

        }

    }

  
    
  
    
}





- (void)webView:(WKWebView *)webView didFinishNavigation:(null_unspecified WKNavigation *)navigation {
    
    [webView evaluateJavaScript:@"Math.max(document.body.scrollHeight, document.body.offsetHeight, document.documentElement.clientHeight, document.documentElement.scrollHeight, document.documentElement.offsetHeight)"
              completionHandler:^(id _Nullable result, NSError * _Nullable error) {
                  if (!error) {
                      // do with the height
                      NSNumber *number = result;
                      
//                      NSLog(@"%f======",number.floatValue);
                      
                      self.webView.height = number.floatValue;
                      
                      self.wkwebBgH.constant = number.floatValue;
                      
                      [self layoutIfNeeded];
                      
                      GoodDetailController *vc = (GoodDetailController *)self.viewController;
                      
                      [vc.plainTableView reloadData];
                  }
              }];
}



- (void)userContentController:(WKUserContentController *)userContentController didReceiveScriptMessage:(WKScriptMessage *)message{
    
    
    
    
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
